package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 获取订单确认页面所需数据
     *
     * @param tradeVo {itemType:"项目类型",:itemId:"项目",trackCount:"声音数量"}
     * @return 订单vo对象->为后续提交订单准备
     */
    @GuiGuLogin
    @Operation(summary = "获取订单确认页面所需数据")
    @PostMapping("/orderInfo/trade")
    public Result<OrderInfoVo> getOrderTradeData(@RequestBody TradeVo tradeVo) {
        OrderInfoVo orderInfoVo = orderInfoService.getOrderTradeData(tradeVo);
        return Result.ok(orderInfoVo);
    }


    /**
     * 当前用户提交订单（可能包含余额支付）
     *
     * @param orderInfoVo 订单VO信息
     * @return {orderNo:订单唯一编号}
     */
    @GuiGuLogin
    @Operation(summary = "当前用户提交订单（可能包含余额支付）")
    @PostMapping("/orderInfo/submitOrder")
    public Result<Map<String, String>> submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        Map<String, String> map = orderInfoService.submitOrder(orderInfoVo);
        return Result.ok(map);
    }


    /**
     * 查询当前用户指定订单信息(包含订单商品明细，优惠明细)
     *
     * @param orderNo 订单编号
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "查询指定订单信息")
    @GetMapping("/orderInfo/getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }


    /**
     * 查询当前用户订单列表（包含订单商品明细）
     *
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "查询当前用户订单列表（包含订单商品明细）")
    @GetMapping("/orderInfo/findUserPage/{page}/{limit}")
    public Result<Page<OrderInfo>> getUserOrderByPage(@PathVariable int page, @PathVariable int limit) {
        //1.构建分页对象
        Page<OrderInfo> pageInfo = new Page<>(page, limit);
        //2.调用业务逻辑
        orderInfoService.getUserOrderByPage(pageInfo);
        return Result.ok(pageInfo);
    }


    /**
     * 在线付款成功后-更新订单支付状态（新增购买记录）
     * @param orderNo
     * @return
     */
    @Operation(summary = "在线付款成功后-更新订单支付状态（新增购买记录）")
    @GetMapping("/orderInfo/orderPaySuccess/{orderNo}")
    public Result orderPaySuccess(@PathVariable String orderNo){
        orderInfoService.orderPaySuccess(orderNo);
        return Result.ok();
    }
}

